package com.wei.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.entity.Member;
import com.wei.entity.vo.LoginVo;
import com.wei.entity.vo.RegisterVo;
import com.wei.service.MemberService;
import com.wei.mapper.MemberMapper;
import com.wei.service_base.exception.XueYuanException;
import com.wei.utils.JwtUtils;
import com.wei.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author HASEE
 * @description 针对表【ucenter_member(会员表)】的数据库操作Service实现
 * @createDate 2022-12-03 21:43:35
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录
     *
     * @param loginVo
     * @return
     */
    @Override
    public String MemberLogin(LoginVo loginVo) {

        String phone = loginVo.getMobile();
        String password = loginVo.getPassword();

        //不能为空
        if (StrUtil.isEmpty(phone) || StrUtil.isEmpty(password)) {
            throw new XueYuanException(20001, "用户名和密码不能为空");
        }

        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMobile, phone);

        Member member = baseMapper.selectOne(queryWrapper);

        //判断用户是否存在
        if (member == null) {
            throw new XueYuanException(20001, "用户名不存在");
        }

        String pwd = MD5.encrypt(password);

        //判断手机号和密码是否正确
        if (!pwd.equals(member.getPassword())) {
            throw new XueYuanException(20001, "用户名或密码错误");
        }

        //判断账号是否被封
        if (member.getIsDisabled() == 1) {
            throw new XueYuanException(20001, "该账户已被冻结");
        }

        //以上都没出事，就说明验证成功
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;
    }

    //注册
    @Override
    public void memberRegister(RegisterVo registerVo) {

        String code = registerVo.getCode();
        String phone = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();

        if (StrUtil.isEmpty(phone) || StrUtil.isEmpty(password) || StrUtil.isEmpty(nickname)) {
            throw new XueYuanException(20001, "信息不能为空");
        }

        //判断手机号是否已经注册过了
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getMobile, phone);

        boolean exists = baseMapper.exists(queryWrapper);

        if (exists) {
            throw new XueYuanException(20001, "用户已存在");
        }

        //给密码加密
        String pwd = MD5.encrypt(password);

        //判断验证码
        String phoneCode = redisTemplate.opsForValue().get(phone);

        if (!code.equals(phoneCode)) {
            throw new XueYuanException(20001, "验证码错误");
        }

        Member member = new Member();

        member.setMobile(phone);
        member.setPassword(pwd);
        member.setNickname(nickname);
        member.setAvatar("https://edu-xueyuan-1010.oss-cn-shanghai.aliyuncs.com/teacher/gulinaza.jpeg");

        baseMapper.insert(member);
    }

    @Override
    public Member getUserInfo(String openid) {

        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Member::getOpenid, openid);

        Member member = baseMapper.selectOne(queryWrapper);

        return member;
    }

    @Override
    public Integer countRegisterByDay(String day) {
        Integer count = baseMapper.countRegisterData(day);

        return count;
    }
}




